import json

f = open( "20201001cfg.json",'r')
cfg_dict = json.load(f)


crc = cfg_dict[0]['crc']
code = cfg_dict[0]['code']
ap_id	=	int(cfg_dict[0]['ap_id'],16)
s_v =       int(cfg_dict[0]['s_v'],16) 
io_v =       int(cfg_dict[0]['io_v'],16)  
ap_v  =      int(cfg_dict[0]['ap_v'] ,16)                
on_delay	=	cfg_dict[0]['on_delay']
off_to_on_min_time	=	cfg_dict[0]['off_to_on_min_time']
v = cfg_dict[0]['ip0'].split('.')
ip0	= (int(v[0])<<24) + (int(v[1])<<16) + (int(v[2])<<8) + int(v[3])	
port0	=	cfg_dict[0]['port0']
switch0	=	int(cfg_dict[0]['switch0'],16)
timer_data_time_sec0 = cfg_dict[0]['timer_data_time_sec0']                
v = cfg_dict[0]['ip1'].split('.')
ip1	= (int(v[0])<<24) + (int(v[1])<<16) + (int(v[2])<<8) + int(v[3])	
port1	=	cfg_dict[0]['port1']
switch1	=	int(cfg_dict[0]['switch1'],16)
timer_data_time_sec1 = cfg_dict[0]['timer_data_time_sec1']               
v = cfg_dict[0]['ip2'].split('.')
ip2	= (int(v[0])<<24) + (int(v[1])<<16) + (int(v[2])<<8) + int(v[3])	
port2	=	cfg_dict[0]['port2']
switch2	=	int(cfg_dict[0]['switch2'],16)
timer_data_time_sec2 = cfg_dict[0]['timer_data_time_sec2']
sensor_count	=	cfg_dict[0]['sensor_count']

data = struct.pack('<IIHHHHHIHBHIHBHIHBHB', code, ap_id, s_v, io_v, ap_v, on_delay, off_to_on_min_time, ip0, port0, switch0, timer_data_time_sec0, ip1, port1, switch1, timer_data_time_sec1, ip2, port2, switch2, timer_data_time_sec2, sensor_count)            

for i in range(sensor_count):
    sesnor_id=	int(cfg_dict[i+1]['sensor_id'],16)
    mode=	int(cfg_dict[i+1]['mode'],16)
    on_delay_s=	cfg_dict[i+1]['on_delay_s']
    off_to_on_min_time_s=	cfg_dict[i+1]['off_to_on_min_time_s']
    lane=	cfg_dict[i+1]['lane']
    lane_index = cfg_dict[i+1]['lane_index']                
    distance=	cfg_dict[i+1]['distance']
    direction = cfg_dict[i+1]['direction']
    lane_direction=	cfg_dict[i+1]['lane_direction']
    data1 = struct.pack('<HBHHBBHBB', sesnor_id, mode, on_delay_s, off_to_on_min_time_s, lane, lane_index, distance, direction,lane_direction)
    data += data1

